package com.winway.sixbook.ui;

import com.winway.sixbook.R;
import com.winway.sixbook.custom.CloseActivityClass;
import com.winway.sixbook.service.SeedManager;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class SeedChooseActivity extends Activity {
	
	private static final String TAG = "SeedChooseActivity";
	ListView listView = null;
	private SeedManager mseedManager;
	private TextView typelistTextView;
	private TextView typeidlistTextView;
	
	private ImageButton top_tbn_fanhui;
	private Button top_tbn_xinzeng;
	private TextView title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_seed_choose);
		CloseActivityClass.activityList.add(this);
		
		if (mseedManager == null) {
			mseedManager = new SeedManager(this);
			mseedManager.openDataBase();
        }
		listView = (ListView)findViewById(R.id.seedchoose_typelist);
		top_tbn_fanhui = (ImageButton)findViewById(R.id.nagivate_btn_left);
        top_tbn_xinzeng = (Button)findViewById(R.id.navigate_btn_right);
        title = (TextView)findViewById(R.id.title);
        
        top_tbn_xinzeng.setOnClickListener(mListener);
		top_tbn_fanhui.setOnClickListener(mListener);
        title.setText("种子类型设置");
        
		Cursor cursor = mseedManager.fetchAllSeedDatas();
        startManagingCursor(cursor);
         
        ListAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.typelist_item,
        		cursor,
        		new String[]{"seed_name","_id"},
        		new int[]{R.id.typelist_item_digest,R.id.typeidlist_item_digest});
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				typelistTextView = (TextView)arg1.findViewById(R.id.typelist_item_digest);
				typeidlistTextView = (TextView)arg1.findViewById(R.id.typeidlist_item_digest);
				Intent addintent = new Intent(SeedChooseActivity.this, SeedAddActivity.class);
				Bundle bundle = new Bundle(); 
				bundle.putString("typeid", ""+typeidlistTextView.getText());
				bundle.putString("typename", ""+typelistTextView.getText());
				addintent.putExtras(bundle); 
				startActivity(addintent);
				finish();
			}  
			
        });  
	}
	
	OnClickListener mListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.nagivate_btn_left:
					Intent counter1 = new Intent(SeedChooseActivity.this, SetActivity.class);
					startActivity(counter1);
					finish();
					break;
				case R.id.navigate_btn_right:
					Intent counter2 = new Intent(SeedChooseActivity.this, SeedAddActivity.class);
					Bundle bundle = new Bundle(); 
					bundle.putString("typeid", "");
					bundle.putString("typename", "");
					counter2.putExtras(bundle); 
					startActivity(counter2);
					finish();
					break;
			}
		}
	};
	
	@Override
	protected void onResume() {
		if (mseedManager == null) {
			mseedManager = new SeedManager(this);
			mseedManager.openDataBase();
        }
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (mseedManager != null) {
			mseedManager.closeDataBase();
			mseedManager = null;
        }
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mseedManager != null) {
			mseedManager.closeDataBase();
			mseedManager = null;
        }
		super.onPause();
	}
	
	/**
	 * 捕捉back
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent counter1 = new Intent(SeedChooseActivity.this, SetActivity.class);
			startActivity(counter1);
			finish();
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}

}
